Android HAL 硬件抽象层加载过程

硬件厂商处于保护核心代码,会将核心实现以so库的形式出现在HAL层,当需要时HAL会自动调用相关的共享库。

共享库的格式

<MODULE_ID>.variant.so

  • id: 为硬件模块的唯一编号
  • variant:为变种名称。这个值从系统属性中获取。获取顺序保存在variant_keys数组中。
static const char *variant_keys[] = {
    "ro.hardware",  /* This goes first so that it can pick up a different
                       file on the emulator. */
    "ro.product.board",
    "ro.board.platform",
    "ro.arch"
};

流程
app通过jni调用hal层hw_get_module函数获取硬件模块,hw_get_module通过模块ID查询对应的模块的共享库,调用load打开共享库,获取硬件结构地址,根据固定符号HAL_MODULE_INFO_SYM查找结构体hw_module_t,调用hw_module_methods_t中的open方法打开硬件。在open时传入hw_device_t二级指针,将模块的操作函数保存在hw_device_t中,实现与硬件的交互。

hw_get_module

int hw_get_module_by_class(const char *class_id, const char *inst,
                           const struct hw_module_t **module)
{
    int i = 0;
    char prop[PATH_MAX] = {0};
    char path[PATH_MAX] = {0};
    char name[PATH_MAX] = {0};
    char prop_name[PATH_MAX] = {0};

    if (inst)
        snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
    else
        strlcpy(name, class_id, PATH_MAX);

    /*
     * Here we rely on the fact that calling dlopen multiple times on
     * the same .so will simply increment a refcount (and not load
     * a new copy of the library).
     * We also assume that dlopen() is thread-safe.
     */

    /* First try a property specific to the class and possibly instance */
    snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);
    if (property_get(prop_name, prop, NULL) > 0) {  //??获取属相
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {    //检测模块是否存在
            goto found;
        }
    }

    //在hal层搜索动态共享库的方式
    /* Loop through the configuration variants looking for a module */
    for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
        if (property_get(variant_keys[i], prop, NULL) == 0) {
            continue;
        }
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
            goto found;
        }
    }

    //最后尝试默认库
    /* Nothing found, try the default */
    if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
        goto found;
    }

    return -ENOENT;

found:
    /* load the module, if this fails, we're doomed, and we should not try
     * to load a different variant. */
    return load(class_id, path, module); ////装载库,得到module
}

//根据硬件id,获取id对应的硬件模块结构体
int hw_get_module(const char *id, const struct hw_module_t **module)
{   
    //id将作为路径一部分,下面代码的name
    return hw_get_module_by_class(id, NULL, module);
}
  • hw_get_module 调用hw_get_module_by_class完成加载过程
  • hw_get_module_by_class根据传入的变量class_id,查询ro.hardware.<id>获取属相值,如果存在作为variant值,调用hw_module_exit检查目标共享库是否存在,存在调用load加载
  • 不存在,循环遍历variant_keys数组定义的key获取对应的属性值,并判断是否存在对应的共享库,存在调用load加载,否则返回错误

hw_module_exists方法根据拼接的路径path,去查询是否存在共享库。共享库的路径由HAL_LIBRARY_PATH1(系统存放路径) ,id(moudle ID),variant(属性)组成。

共享库存放的路径

#if defined(__LP64__)
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#else
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
#endif
  • 共享库存放的位置位于 /system/lib64/hw,/system/lib/hw 和/vendor/lib64/hw ,/vendor/lib/hw 路径下
  • 共享库以<MODULE_ID>.variant.so命名,id为模块名称,variant为变种名称,随系统平台变化。

load

static int load(const char *id,
        const char *path,
        const struct hw_module_t **pHmi)
{
    int status = -EINVAL;
    void *handle = NULL;
    struct hw_module_t *hmi = NULL;

    /*
     * load the symbols resolving undefined symbols before
     * dlopen returns. Since RTLD_GLOBAL is not or'd in with
     * RTLD_NOW the external symbols will not be global
     */
     //调用dlopen打开path定义的目标共享库,得到库文件的句柄handle
    handle = dlopen(path, RTLD_NOW);
    if (handle == NULL) {
        char const *err_str = dlerror();
        ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
        status = -EINVAL;
        goto done;
    }

    /* Get the address of the struct hal_module_info. */
    const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
    //调用dlsym获取符号HAL_MODULE_INFO_SYM_AS_STR的地址,赋值给hmi
    hmi = (struct hw_module_t *)dlsym(handle, sym);
    if (hmi == NULL) {
        ALOGE("load: couldn't find symbol %s", sym);
        status = -EINVAL;
        goto done;
    }

    /* Check that the id matches */
    if (strcmp(id, hmi->id) != 0) {
        ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        status = -EINVAL;
        goto done;
    }
    //保存共享库句柄
    hmi->dso = handle;

    /* success */
    status = 0;

    done:
    if (status != 0) {
        hmi = NULL;
        if (handle != NULL) {
            dlclose(handle);
            handle = NULL;
        }
    } else {
        ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
                id, path, *pHmi, handle);
    }

    //返回得到的hw_module_t结构体的指针
    *pHmi = hmi;

    return status;
}
  • load 函数调用dlopen打开目标模块共享库,使用dlsym获取HMI地址(HMI为模块hw_module_t结构体的名字),得到对应模块hw_module_t指针;通过hw_module_t指针,可以对硬件进行操作。

通过dlopen和dlsym获取模块hw_module_t指针,操作硬件。

蓝牙HAL加载

硬件抽象层中每个模块必须自定义一个硬件抽象层模块结构,第一个成员必须是hw_module_t,其次才是模块的一此相关信息;还必须包含HAL_MODULE_INFO_SYM 。

蓝牙HAL加载在 com_android_bluetooth_btservice_AdapterService.cpp的classInitNative方法中,

static void classInitNative(JNIEnv* env, jclass clazz) {
    int err;
    hw_module_t* module;
    // ........中间省略.....
    char value[PROPERTY_VALUE_MAX];
    property_get("bluetooth.mock_stack", value, "");

    const char *id = (strcmp(value, "1")? BT_STACK_MODULE_ID : BT_STACK_TEST_MODULE_ID);

    //获取蓝牙模块hw_module_t指针
    err = hw_get_module(id, (hw_module_t const**)&module);

    if (err == 0) {
        hw_device_t* abstraction;
    //调用open方法,获取蓝牙设备hw_device_t指针
        err = module->methods->open(module, id, &abstraction);
        if (err == 0) {
            //蓝牙实现中将蓝牙设备bluetooth_device_t 和 bluetooth_module_t 定义成同一个值
            bluetooth_module_t* btStack = (bluetooth_module_t *)abstraction;
      //获取蓝牙模块interface接口,通过sBluetoothInterface操作蓝牙设备
            sBluetoothInterface = btStack->get_bluetooth_interface();
        } else {
           ALOGE("Error while opening Bluetooth library");
        }
    } else {
        ALOGE("No Bluetooth Library found");
    }
}

BT_STACK_MODULE_ID 定义在bluetooth.h文件中,

 #define BT_HARDWARE_MODULE_ID "bluetooth"
#define BT_STACK_MODULE_ID "bluetooth"

hw_get_module获取蓝牙模块hw_module_t指针,
蓝牙自定义硬件模块hw_module_t定义

struct hw_module_t HAL_MODULE_INFO_SYM = {
    .tag = HARDWARE_MODULE_TAG,
    .version_major = 1,
    .version_minor = 0,
    .id = BT_HARDWARE_MODULE_ID,
    .name = "Bluetooth Stack",
    .author = "The Android Open Source Project",
    .methods = &bt_stack_module_methods
};

蓝牙调用自定义模块hw_module_t中open方法,获取hw_device_t指针,

static struct hw_module_methods_t bt_stack_module_methods = {
    .open = open_bluetooth_stack,
};

static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) {
  //给bluetooth_device_t赋值
  static bluetooth_device_t device = {
    //common变量赋值
    .common = {     
      .tag = HARDWARE_DEVICE_TAG,
      .version = 0,
      .close = close_bluetooth_stack,
    },
    //获取蓝牙模块接口
    .get_bluetooth_interface = bluetooth__get_bluetooth_interface
  };

  device.common.module = (struct hw_module_t *)module;
  //将bluetooth_device_t指针强制转换为hw_device_t指针赋值给abstraction
  *abstraction = (struct hw_device_t *)&device;
  return 0;
}

open_bluetooth_stack创建bluetooth_device_t 结构体,初始化,转换成hw_device_t。module->methods->open得到bluetooth_device_t 结构体的指针,也是bluetooth_module_t结构体的指针。

typedef struct {
    struct hw_device_t common;
    const bt_interface_t* (*get_bluetooth_interface)();
} bluetooth_device_t;

typedef bluetooth_device_t bluetooth_module_t;

bluetooth_device_t 包含hw_device_t;bluetooth_device_t 重命名为bluetooth_module_t。

open方法(open_bluetooth_stack)获取蓝牙设备hw_device_t指针,转化为bluetooth_module_t 指针,在open_bluetooth_stack中将bluetooth_get_bluetooth_interface赋值给get_bluetooth_interface,再赋值给sBluetoothInterface。

bluetooth_get_bluetooth_interface定义了蓝牙模块的基本接口,bluetooth_get_bluetooth_interface最后赋值给sBluetoothInterface,调用sBluetoothInterface可以操作蓝牙模块,与硬件交互。具体接口实现和平台相关。

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容